package com.rem.rabbitmq.boot.Elazy;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 惰性队列
 * 在发送1百万条消息，每条消息大概占1KB的情况下，普通队列占用内存是1.2GB，而惰性队列仅仅占用1.5MB
 *
 * @author Rem
 * @date 2021-12-29
 */

@Configuration
public class LazyConfig {

    public static final String LAZY_EXCHANGE = "LAZY_EXCHANGE";

    public static final String LAZY_ROUTING_KEY = "LAZY_ROUTING_KEY";

    public static final String LAZY_QUEUE = "LAZY_QUEUE";


    @Bean("lazyExchange")
    public DirectExchange lazyExchange() {
        return ExchangeBuilder.directExchange(LAZY_EXCHANGE).build();
    }

    /**
     * 声明队列为惰性队列 消息直接存放在磁盘中
     *
     * @return
     */
    @Bean("lazyQueue")
    public Queue lazyQueue() {
        return QueueBuilder.durable(LAZY_QUEUE).lazy().build();
    }

    @Bean
    public Binding lazyQueueaBinding(@Qualifier("lazyQueue") Queue lazyQueue, @Qualifier("lazyExchange") DirectExchange lazyExchange) {
        return BindingBuilder.bind(lazyQueue).to(lazyExchange).with(LAZY_ROUTING_KEY);
    }


}
